from PyQt4 import QtGui, QtCore

import language
from pluginmanager import download_descriptor, download_plugin

class AddPlugins(QtGui.QDialog):
    
    def __init__(self, pare, plugs, plugsFolder):
        QtGui.QDialog.__init__(self, pare)
        self._parent = pare
        lang = self._parent.pyTray.lang
        self.setWindowTitle(lang['addPlugWindow'])
        self.setFixedWidth(720)
        v_box = QtGui.QVBoxLayout(self)
        
        self._actualPlugins = plugs
        self._plugins = download_descriptor()
        self._folder = plugsFolder
        
        #Table
        self._table = QtGui.QTableWidget(1, 2)
        self._table.setSelectionBehavior(QtGui.QAbstractItemView.SelectRows)
        self._table.removeRow(0)
        header = [lang['tablePlugin'][1], lang['tablePlugin'][2]]
        self._table.setHorizontalHeaderLabels(header)
        self._table.setColumnWidth(0, 210)
        self._table.setColumnWidth(1, 450)
        self._table.horizontalHeader().setStretchLastSection(True)
        
        #Complete Table
        for p in self._actualPlugins:
            names = [pl[0] for pl in self._plugins]
            if p._name in names:
                pos = names.index(p._name)
                del self._plugins[pos]
        self._set_data()
        
        #Buttons
        self._download = QtGui.QPushButton(lang['download'])
        self._cancel = QtGui.QPushButton(lang['cancel'])
        h_box = QtGui.QHBoxLayout()
        h_box.addWidget(self._cancel)
        h_box.addWidget(self._download)
        
        #add Widgets
        v_box.addWidget(self._table)
        v_box.addLayout(h_box)
        
        #Singla->Slot
        self.connect(self._download, QtCore.SIGNAL("clicked()"), self._download_selected_plugin)
        self.connect(self._cancel, QtCore.SIGNAL("clicked()"), self.hide)
        
    def _set_data(self):
        r = 0
        for plug in self._plugins:
            self._table.insertRow(r)
            #Column 1
            item = QtGui.QTableWidgetItem(plug[1])
            item.setFlags( QtCore.Qt.ItemIsSelectable |  QtCore.Qt.ItemIsEnabled )
            self._table.setItem(r, 1, item)
            #Column 0
            item = QtGui.QTableWidgetItem(plug[0])
            item.setFlags( QtCore.Qt.ItemIsSelectable |  QtCore.Qt.ItemIsEnabled )
            self._table.setItem(r, 0, item)
            r += 1
            
    def _download_selected_plugin(self):
        row = self._table.currentRow()
        if row > -1:
            self._download.setEnabled(False)
            file = self._plugins[row][2]
            download_plugin(file, self._folder)
            self._download.setEnabled(True)
            del self._plugins[row]
            self._reload_table()
            #Reload plugins in PyTv
            self._parent.reload_data()
            
    def _reload_table(self):
        while self._table.rowCount() > 0:
            self._table.removeRow(0)
        self._set_data()

    def closeEvent(self, event):
        event.ignore()
        self.done(0)
